home *** CD-ROM | disk | FTP | other *** search
/ Multimedia Jumpstart / Multimedia Microsoft Jumpstart Version 1.1a (Microsoft).BIN / develpmt / source / sprites / palette.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-12  |  6.3 KB  |  268 lines

  1. /*
  2.  
  3.     palette.c
  4.  
  5.  
  6.     DIB palette routines
  7.  
  8. */
  9.  
  10. #include "global.h"
  11.  
  12. HPALETTE CreateWinDIBPalette(LPBITMAPINFO lpBmpInfo);
  13. HPALETTE CreatePMDIBPalette(LPBITMAPINFO lpBmpInfo);
  14.  
  15. //
  16. // Create a palette for a DIB
  17. //
  18.  
  19. HPALETTE CreateDIBPalette(LPBITMAPINFO lpBmpInfo)
  20. {
  21.     LPBITMAPINFOHEADER lpBmpInfoHdr;
  22.     HANDLE hPalMem;
  23.     LOGPALETTE *pPal;
  24.     HPALETTE hPal;
  25.     LPRGBQUAD lpRGB;
  26.     int iColors, i;
  27.  
  28.     //
  29.     // validate the header
  30.     //
  31.  
  32.     lpBmpInfoHdr = (LPBITMAPINFOHEADER) lpBmpInfo;
  33.     if (!IsWinDIB(lpBmpInfoHdr)) {
  34.         dprintf1("Invalid lpBmpInfoHdr");
  35.         return NULL;
  36.     }
  37.  
  38.     //
  39.     // get a pointer to the RGB quads and the number of colors
  40.     // in the color table (we don't do 24 bit stuff here)
  41.     //
  42.  
  43.     lpRGB = (LPRGBQUAD)((LPSTR)lpBmpInfoHdr + (WORD)lpBmpInfoHdr->biSize);
  44.  
  45.     iColors = NumDIBColorEntries(lpBmpInfo);
  46.  
  47.     //
  48.     // Check we got a color table
  49.     //
  50.  
  51.     if (!iColors) {
  52.         dprintf1("No color table");   
  53.         return NULL;
  54.     }
  55.  
  56.     //
  57.     // allocate a log pal and fill it with the color table info
  58.     //
  59.  
  60.     hPalMem = LocalAlloc(LMEM_MOVEABLE, sizeof(LOGPALETTE) + iColors * sizeof(PALETTEENTRY));
  61.     if (!hPalMem) {
  62.         dprintf1("Out of local memory for logpal");
  63.         return NULL;
  64.     }
  65.     pPal = (LOGPALETTE *) LocalLock(hPalMem);
  66.     pPal->palVersion = 0x300; // Windows 3.0
  67.     pPal->palNumEntries = iColors; // table size
  68.     for (i=0; i<iColors; i++) {
  69.         pPal->palPalEntry[i].peRed = lpRGB[i].rgbRed;
  70.         pPal->palPalEntry[i].peGreen = lpRGB[i].rgbGreen;
  71.         pPal->palPalEntry[i].peBlue = lpRGB[i].rgbBlue;
  72.         pPal->palPalEntry[i].peFlags = 0;
  73.     }
  74.  
  75.     hPal = CreatePalette(pPal);
  76.     LocalUnlock(hPalMem);
  77.     LocalFree(hPalMem);
  78.  
  79.     return hPal;
  80. }
  81.  
  82. HPALETTE CopyPalette(HPALETTE hSrcPal) 
  83. {
  84.     HANDLE hPalMem;
  85.     LOGPALETTE *pPal;
  86.     HPALETTE hDstPal;
  87.     int iEntries;
  88.  
  89.  
  90.     GetObject(hSrcPal, sizeof(iEntries), (LPSTR)&iEntries); // get no. of pal colors
  91.     if (!iEntries) return NULL;
  92.  
  93.     hPalMem = LocalAlloc(LMEM_MOVEABLE, sizeof(LOGPALETTE) + iEntries * sizeof(PALETTEENTRY));
  94.     if (!hPalMem) {
  95.         dprintf1("\nOut of local memory for logpal");
  96.         return NULL;
  97.     }
  98.     pPal = (LOGPALETTE *) LocalLock(hPalMem);
  99.     pPal->palVersion = 0x300; // Windows 3.0
  100.     pPal->palNumEntries = iEntries; // table size
  101.     GetPaletteEntries(hSrcPal, 0, iEntries, pPal->palPalEntry);
  102.  
  103.     hDstPal = CreatePalette(pPal);
  104.     LocalUnlock(hPalMem);
  105.     LocalFree(hPalMem);
  106.  
  107.     return hDstPal;
  108. }
  109.  
  110. //
  111. // Modify a palette to have the system colors in the first 
  112. // and last 10 positions
  113. //
  114.  
  115. void SetSysPalColors(HPALETTE hPal)
  116. {
  117.     HANDLE hPalMem;
  118.     LOGPALETTE *pPal;
  119.     int iEntries;
  120.     HDC hdcScreen;
  121.  
  122.     dprintf2("SetSysPalColors");
  123.  
  124.     //
  125.     // Create a log palette with 256 entries
  126.     //
  127.  
  128.     hPalMem = LocalAlloc(LMEM_MOVEABLE, 
  129.                          sizeof(LOGPALETTE) + 256 * sizeof(PALETTEENTRY));
  130.     if (!hPalMem) {
  131.         dprintf1("\nOut of local memory for logpal");
  132.         return;
  133.     }
  134.  
  135.     //
  136.     // Set the palette info from the supplied palette
  137.     //
  138.  
  139.     pPal = (LOGPALETTE *) LocalLock(hPalMem);
  140.     pPal->palVersion = 0x300; // Windows 3.0
  141.     GetObject(hPal, sizeof(iEntries), (LPSTR)&iEntries);
  142.     pPal->palNumEntries = iEntries; // table size
  143.     GetPaletteEntries(hPal, 0, iEntries, pPal->palPalEntry);
  144.  
  145. #ifdef DEBUG
  146.     {
  147.         int i;
  148.  
  149.         dprintf3(" Before...");
  150.         for (i=0; i<10; i++) {
  151.             dprintf3("  %d  %u,%u,%u",
  152.                     i,
  153.                     pPal->palPalEntry[i].peRed,
  154.                     pPal->palPalEntry[i].peGreen,
  155.                     pPal->palPalEntry[i].peBlue);
  156.         }
  157.         for (i=246; i<256; i++) {
  158.             dprintf3("  %d  %u,%u,%u",
  159.                     i,
  160.                     pPal->palPalEntry[i].peRed,
  161.                     pPal->palPalEntry[i].peGreen,
  162.                     pPal->palPalEntry[i].peBlue);
  163.         }
  164.     }
  165. #endif // DEBUG
  166.  
  167.     //
  168.     // Copy the low 10 and high ten system palette entries
  169.     //
  170.  
  171.     hdcScreen = GetDC(NULL);
  172.     GetSystemPaletteEntries(hdcScreen, 0, 10, pPal->palPalEntry);
  173.     GetSystemPaletteEntries(hdcScreen, 246, 10, &(pPal->palPalEntry[246]));
  174.     ReleaseDC(NULL, hdcScreen);
  175.  
  176. #ifdef DEBUG
  177.     {
  178.         int i;
  179.  
  180.         dprintf3(" After...");
  181.         for (i=0; i<10; i++) {
  182.             dprintf3("  %d  %u,%u,%u",
  183.                     i,
  184.                     pPal->palPalEntry[i].peRed,
  185.                     pPal->palPalEntry[i].peGreen,
  186.                     pPal->palPalEntry[i].peBlue);
  187.         }
  188.         for (i=245; i<256; i++) {
  189.             dprintf3("  %d  %u,%u,%u",
  190.                     i,
  191.                     pPal->palPalEntry[i].peRed,
  192.                     pPal->palPalEntry[i].peGreen,
  193.                     pPal->palPalEntry[i].peBlue);
  194.         }
  195.     }
  196. #endif // DEBUG
  197.  
  198.     //
  199.     // Write the modified entries back to the palette
  200.     //
  201.  
  202.     SetPaletteEntries(hPal, 0, 256, pPal->palPalEntry);
  203.  
  204.  
  205.     LocalUnlock(hPalMem);
  206.     LocalFree(hPalMem);
  207.  
  208. }
  209.  
  210. //
  211. // Process palette messages
  212. //
  213.  
  214. LRESULT PaletteMessage(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) 
  215. {
  216.     HDC hDC;
  217.     int i;
  218.     HPALETTE hOldPal;
  219.  
  220.  
  221.     switch (msg) {
  222.     case WM_PALETTECHANGED:
  223.  
  224.         //
  225.         // Someone changed the palette. See if was us.
  226.         //
  227.  
  228.         if ((HWND)wParam == hWnd) {
  229.  
  230.             return (LRESULT) 0; // nothing to do (it was us)
  231.         }
  232.  
  233.         // else drop through
  234.  
  235.     case WM_QUERYNEWPALETTE:
  236.  
  237.         //
  238.         // We are going active so realize our palette
  239.         //
  240.  
  241.         if (hpalCurrent) {
  242.             dprintf2("Realizing palette");
  243.             hDC = GetDC(hWnd);
  244.             hOldPal = SelectPalette(hDC, hpalCurrent, 0);
  245.             i = RealizePalette(hDC);
  246.             ReleaseDC(hWnd, hDC);
  247.             if (i > 0) {
  248.  
  249.                 //
  250.                 // some colors changed so we need to do a repaint
  251.                 //
  252.  
  253.                 dprintf2("Repainting with new palette");
  254.                 InvalidateRect(hWnd, NULL, TRUE); // repaint the lot
  255.                 return TRUE; // say we did something
  256.             }
  257.         }
  258.  
  259.         break;
  260.  
  261.     default:
  262.         break;
  263.     }
  264.  
  265.     return 0l; // say we did nothing
  266. }
  267.  
  268.